package com.louis.kitty.admin.sevice.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.louis.kitty.admin.dao.SysDeptMapper;
import com.louis.kitty.admin.model.SysDept;
import com.louis.kitty.admin.sevice.SysDeptService;
import com.louis.kitty.core.page.MybatisPageHelper;
import com.louis.kitty.core.page.PageRequest;
import com.louis.kitty.core.page.PageResult;

@Service
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Override
	public int save(SysDept record) {
		if(record.getId() == null || record.getId() == 0) {
			return sysDeptMapper.insertSelective(record);
		}
		return sysDeptMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int delete(SysDept record) {
		return sysDeptMapper.deleteByPrimaryKey(record.getId());
	}

	@Override
	public int delete(List<SysDept> records) {
		for(SysDept record:records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public SysDept findById(Long id) {
		return sysDeptMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest) {
		return MybatisPageHelper.findPage(pageRequest, sysDeptMapper);
	}

	@Override
	public List<SysDept> findTree() {
		List<SysDept> sysDepts = new ArrayList<>();
		List<SysDept> depts = sysDeptMapper.findAll();
		for (SysDept dept : depts) {
			if (dept.getParentId() == null || dept.getParentId() == 0) {
				dept.setLevel(0);
				sysDepts.add(dept);
			}
		}
		findChildren(sysDepts, depts);
		return sysDepts;
	}
	@Override
	//根据当前id查询该id及以下机构树
	public List<SysDept> findDeptTreeById(PageRequest pageRequest){
		//提出deptid
		String idstr=pageRequest.getColumnFilter("parentdeptid").getValue();
		Long id=Long.valueOf(idstr);
		List<SysDept> sysDepts = new ArrayList<>();
		SysDept parentdept = sysDeptMapper.selectByPrimaryKey(id);
		//根据当前id查询该id及以下机构链表
		List<SysDept> depts = findChildrenDeptByparentdept(parentdept);
		//设置起始节点
		for (SysDept dept : depts) {
			if (dept.getId() == id) {
				dept.setLevel(0);
				sysDepts.add(dept);
			}
		}
		//排树
		findChildren(sysDepts, depts);
		int i=0;
		return sysDepts;
	}

	//根据当前id查询该id及以下机构链表
	public List<SysDept> findChildrenDeptByparentdept(SysDept parentdept) {
		List<SysDept> deptlist = new ArrayList();
		findChildrenDept(deptlist,parentdept);
//		System.out.println("currentenginids="+enginids);
		return deptlist;
	}

	//递归获取所有子节点的工具方法,for findChildrenDeptByparentdept
	public List<SysDept> findChildrenDept(List<SysDept> deptlist,SysDept parentdept){
//		System.out.println("currentids="+ids);
//		System.out.println("currentid="+pid);
//		System.out.println("currentChildrenids="+sysDeptMapper.findChildrenidById(pid));
		if(deptlist==null){
			deptlist.add(parentdept);
//			System.out.println("put id="+pid);
//			System.out.println("Newids="+ids);
			return deptlist;
		}
		List<SysDept> currentChildrenids=sysDeptMapper.findChildrendeptBydept(parentdept);
		for(SysDept i : currentChildrenids){
			findChildrenDept(deptlist,i);
		}
		deptlist.add(parentdept);
//		System.out.println("put id="+pid);
//		System.out.println("Newids="+ids);
		return deptlist;
	}



	//findTree通过findChildren排列机构树
	private void findChildren(List<SysDept> sysDepts, List<SysDept> depts) {
		for (SysDept sysDept : sysDepts) {
			List<SysDept> children = new ArrayList<>();
			for (SysDept dept : depts) {
				if (sysDept.getId() != null && sysDept.getId().equals(dept.getParentId())) {
					dept.setParentName(dept.getName());
					dept.setLevel(sysDept.getLevel() + 1);
					children.add(dept);
				}
			}
			sysDept.setChildren(children);
			findChildren(children, depts);
		}
	}



	//持久层中的findChildrenidById只是返回下一级的子节点而不是所有的子节点，这里的findChildrenidById负责找出所有的子节点
	public List<Long> findChildrenidById(Long id) {
		List<Long> enginids = new ArrayList();
		findChildrenid(enginids,id);
//		System.out.println("currentenginids="+enginids);
		return enginids;
	}
	//递归获取所有子节点的工具方法
	public List<Long> findChildrenid(List<Long> ids,Long pid){
//		System.out.println("currentids="+ids);
//		System.out.println("currentid="+pid);
//		System.out.println("currentChildrenids="+sysDeptMapper.findChildrenidById(pid));
		if(ids==null){
			ids.add(pid);
//			System.out.println("put id="+pid);
//			System.out.println("Newids="+ids);
			return ids;
		}
		List<Long> currentChildrenids=sysDeptMapper.findChildrenidById(pid);
			for(Long i : currentChildrenids){
				findChildrenid(ids,i);
			}
		ids.add(pid);
//		System.out.println("put id="+pid);
//		System.out.println("Newids="+ids);
		return ids;
	}

}
